#include <Windows.h>
#include <tchar.h>


LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static int  nStep = 0;
	static HWND hButton;
switch (uMsg)
{
	case WM_NCCREATE:
	{
		TCHAR szInfo[256];
		CREATESTRUCT*	pcs	= (CREATESTRUCT*)lParam;
		_stprintf(szInfo, 
				  _T("\nCreate Step: %d (WM_NCCREATE) lParam: [Instance: 0x%0X  CreateParams: %d  Size: (%d, %d, %d, %d)]\n"),
				  ++nStep, pcs->hInstance, pcs->lpCreateParams, pcs->x, pcs->y, pcs->cx, pcs->cy);
		OutputDebugString(szInfo);

		break;
	}
	case WM_CREATE:
	{
		TCHAR szInfo[256];
		CREATESTRUCT*	pcs	= (CREATESTRUCT*)lParam;
		_stprintf(szInfo, 
				  _T("\nCreate Step: %d (WM_CREATE) lParam: [Instance: 0x%0X  CreateParams: %d  Size: (%d, %d, %d, %d)]\n"),
				  ++nStep, pcs->hInstance, pcs->lpCreateParams, pcs->x, pcs->y, pcs->cx, pcs->cy);
		OutputDebugString(szInfo);

		//--: Create sub control
		hButton = CreateWindow(_T("bUTTON"), _T("&Exit"), WS_VISIBLE|WS_CHILD, 0, 0, 100, 50, hWnd, (HMENU)1001, NULL, 0);

		break;
	}
	case WM_SHOWWINDOW:
	{
		TCHAR szInfo[256];
		_stprintf(szInfo, 
				  _T("\nCreate Step: %d (WM_SHOWWINDOW)\n"),
			      ++nStep);
		OutputDebugString(szInfo);

		break;
	}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}

		case WM_COMMAND:
			{
				if (wParam == 1001)
				{
					SendMessage(hWnd, WM_CLOSE, 0, 0);
					break;
				}
				break;
			}
	}

	return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}

bool RegisteWnd(HINSTANCE hInstance, const TCHAR* szWndClass)
{
	WNDCLASS wc			= {0};
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wc.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wc.hIcon			= LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
	wc.hInstance		= hInstance;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= szWndClass;
	wc.lpszMenuName		= NULL;
	wc.style			= CS_HREDRAW|CS_VREDRAW;
	
	return (0 != RegisterClass(&wc));
}

bool CreateMainWnd(HINSTANCE hInstance, DWORD dwStyle, CONST TCHAR* szWndClass, const TCHAR* szWndCaptoin)
{
	bool		bCreated = false;
	WNDCLASS	wc		 = {0};
	if (NULL != szWndClass && 0 != GetClassInfo(hInstance, szWndClass, &wc))
	{
		HWND hWnd = CreateWindow(szWndClass, szWndCaptoin, dwStyle, 0, 0, 800, 480, NULL, NULL, hInstance, (LPVOID)7000);
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			bCreated	= true;
		}
	}
	
	return bCreated;
}

void RunMessageLoop()
{
	MSG Msg;
	while (GetMessage(&Msg, NULL, 0, 0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szWndClass		= _T("Win32APP");
	const TCHAR* szWndCaption	= _T("Win32 APP");

	DWORD dwStyle = WS_VISIBLE|WS_OVERLAPPEDWINDOW|WS_VISIBLE;  //WS_VISIBLE|WS_POPUPWINDOW|WS_DLGFRAME

	if (RegisteWnd(hInstance, szWndClass))
	{
		if (CreateMainWnd(hInstance, dwStyle, szWndClass, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return true;
}